StringBuffer s = new StringBuffer();
StringBuffer s = new StringBuffer(10);
StringBuffer s = new StringBuffer("DockerTpoint");
The append() method of the StringBuffer class concatenates the given argument with this string.
// Java example of String length() Methods
public class Main { public static void main(String args[]){ StringBuffer sb = new StringBuffer("Exam"); sb.append("Deva"); // original string is changed System.out.println(sb); } }
Output:
DockerTpoint
The insert() method of the StringBuffer class inserts the given string at the given position with this string.
// Java example of String insert() Method
public class Main { public static void main(String args[]){ StringBuffer sb = new StringBuffer("Exam"); sb.insert(4,"Deva"); // original string is now changed System.out.println(sb); } }
Output:
DockerTpoint
The delete() method of the StringBuffer class remove the specified string from the begin Index and end Index-1.
// Java example of String delete Method
public class Main { public static void main(String args[]){ StringBuffer sb = new StringBuffer("DockerTpoint"); sb.delete(0,4); // original string is now changed System.out.println(sb); } }
Output:
mDeva
The substring method of the StringBuffer class replaces the given string between begin Index and end Index-1.
// Java example of String substring (int start, int end) Method
public class Main { public static void main(String args[]){ StringBuffer sb = new StringBuffer("E Deva"); sb.replace(1,3,"xam"); // it will replace white space, original string is now changed System.out.println(sb); } }
Output:
DockerTpoint
The reverse method of the StringBuffer class reverses the current string.
// Java example of String concat Method
public class Main { public static void main(String args[]){ StringBuffer sb = new StringBuffer("DockerTpoint"); sb.reverse(); System.out.println(sb); } }
Output:
aveDmaxE
The capacity method of the StringBuffer class returns the buffer's current capacity. The buffer's default capacity is sixteen. If the number of characters exceeds the current capacity, the capacity is increased by (oldcapacity*2)+2.
// Java example of String indexOf Method
public class Main { public static void main(String args[]){ StringBuffer sb = new StringBuffer(); System.out.println(sb.capacity()); // default value 16 sb.append("DockerTpoint"); System.out.println(sb.capacity()); // capicity is 16 sb.append("I Love India"); System.out.println(sb.capacity()); // Now (16*2)+2=34 i.e (oldcapacity*2)+2 } }
Output:
16
16
34
Important methods of StringBuffer class are mention below
Type and Modifier | Method | Description |
---|---|---|
public synchronized StringBuffer | append(String s) | Append(String s) is used to append the specified string to this string. The append() method has several overloads, including append(char), append(boolean), append(int), append(float), append(double) |
public synchronized StringBuffer | insert(int value, String s) | The specific implementation of a method that is already provided by its parent class or superclass is granted by method overriding. |
public synchronized StringBuffer | replace(int begin Index, int end Index-1, String str) | Replace is used to replace the string between the specified begin and end indexes. |
public String | substring(int begin Index, int end Index) | substring returns the substring from the specified beginIndex and endIndex. |
public String | substring(int beginIndex) | Substring returns the substring from the specified begin Index. |
public int | length() | length() returns the length of the string, i.e. the total number of characters. |
public char | charAt(int index) | charAt(int index) returns the character at the specified position. |
public int | capacity() | capacity() returns the current capacity. |
public void | ensureCapacity(int minimumCapacity) | ensureCapacity(int minimumCapacity) ensures that the capacity is at least equal to the specified minimum. |
public synchronized StringBuffer | reverse() | The string can be reversed using reverse(). |
public synchronized StringBuffer | delete(int start Index, int end Index) | The string can be removed from the startIndex and endIndex by using the delete() method. |
public void | setCharAt(int index, char c) | In setCharAt(int index, char c) method the character at the specified index is set to c. |
Post your comment